﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Telerik.Web.UI;
using SMA.GENTEv1;

public partial class Protegidos_Administracao_Cadastros_acSindicatos : System.Web.UI.Page
{
    private const int ItemsPerRequest = 10;
    protected const int idTipoOrgan = 3; // Sindicato

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            CarregaComboStatus(rcbStatus);
            OcultaCampos(true);
        }
    }

    #region Eventos
    protected void btnIncluir_Click(object sender, EventArgs e)
    {
        //Response.Redirect("acCadSindicatos.aspx");
        Server.Transfer("acCadSindicatos.aspx");
    }
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        Pesquisa();
        RadGrid1.DataBind();
        OcultaCampos(false);
    }
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        Pesquisa();
    }
    protected void rcbOrganizacao_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        Organizacao organizacao = new Organizacao();
        List<Organizacao> listOrgan = organizacao.SelectPor(e.Text, idTipoOrgan);

        int itemOffset = e.NumberOfItems;
        int endOffset = Math.Min(itemOffset + ItemsPerRequest, listOrgan.Count);
        e.EndOfItems = endOffset == listOrgan.Count;

        for (int i = itemOffset; i < endOffset; i++)
        {
            rcbOrganizacao.Items.Add(new RadComboBoxItem(listOrgan[i].NomeFantasia, listOrgan[i].IdOrganizacao.ToString()));
        }

        e.Message = GetStatusMessage(endOffset, listOrgan.Count);
    }
    protected void btnLimpar_Click(object sender, EventArgs e)
    {
        Limpar();
    }
    #endregion

    #region Metodos
    private void Pesquisa()
    {
        RadGrid1.DataSource = CarregaOrganizacao(rcbOrganizacao.Text, rcbStatus);
    }
    public List<Organizacao> CarregaOrganizacao(string nome, RadComboBox combo)
    {
        Organizacao organizacao = new Organizacao();
        List<StatusOrganizacao> listStatus = new List<StatusOrganizacao>();

        foreach (RadComboBoxItem item in combo.Items)
        {
            CheckBox chk = (CheckBox)item.FindControl("chk1");
            if (chk.Checked)
                listStatus.Add(new StatusOrganizacao(int.Parse(item.Value)));
        }

        List<Organizacao> listOrgan = organizacao.SelectPor(nome, idTipoOrgan, listStatus);

        return listOrgan;
    }
    protected void CarregaComboStatus(RadComboBox combo)
    {
        StatusOrganizacao statusOrganicao = new StatusOrganizacao();
        List<StatusOrganizacao> listStatus = statusOrganicao.SelectAll();

        combo.DataTextField = "Descricao";
        combo.DataValueField = "IdStatusOrganizacao";
        combo.DataSource = listStatus;
        combo.DataBind();
    }
    private static string GetStatusMessage(int offset, int total)
    {
        if (total <= 0)
            return "No matches";

        return String.Format("Items <b>1</b>-<b>{0}</b> de <b>{1}</b>", offset, total);
    }
    protected void OcultaCampos(bool oculta)
    {
        trResultado.Visible = !oculta;
    }
    protected void Limpar()
    {
        rcbStatus.Text = "";
        rcbOrganizacao.Text = "";
        CarregaComboStatus(rcbStatus);
    }
    #endregion
    
}
